<h1>Позиционный анализ ({$drawing} розыгрышей)</h1>

{php}
    $numbers_frequent = $numbers_by_positions = array();
{/php}

{config module="default" param="numbers" assign="config"}

<table id="positions_result" width="100%">
    <tr>
	<td>
	
	<table id="statistics" border="1">
	    <thead>Статистика</thead>


	    {section name="rows" loop=$config.total+1}
		<tr>
		    {section name="cells" loop=$config.granted+1}
			<td>
			    {assign var="position" value=`$smarty.section.cells.iteration-1`}

			    {if $smarty.section.rows.first}

				{if not $smarty.section.cells.first}
				    #{$position}
				{else}
				    &nbsp;
				{/if}

			    {else}

				{if $smarty.section.cells.first}
				    {assign var="number" value=`$smarty.section.rows.iteration-1`}
				    {$number}
				{else}

				    {assign var="quantity" value=0}
				    {foreach from=$data item="drawing"}

					{if $drawing[$position] == $number}
					{assign var="quantity" value=`$quantity+1`}
					{/if}


				    {/foreach}

				    {$quantity}

				    {php}
					$number = $this->get_template_vars('number');
					$quantity = $this->get_template_vars('quantity');
					if (!array_key_exists($number, $numbers_by_positions) ||
					    $numbers_by_positions[$number]['quantity'] < $quantity)
					{
					    $numbers_by_positions[$number] = array
					    (
						'quantity' => $quantity,
						'position' => $this->get_template_vars('position'),
						'number' => $number,
					    );
					}

					if (!array_key_exists($number, $numbers_frequent)) {
					    $numbers_frequent[$number] = array
					    (
						'quantity' => $quantity,
						'number' => $number,
					    );
					} else {
					    $numbers_frequent[$number]['quantity'] += $quantity;
					}

				    {/php}
				{/if}

			    {/if}
			</td>
		    {/section}
		</tr>
	    {/section}
	</table>

	</td>

	<td valign="top">


{php}
    $config = Kernel_Config::get('default', 'numbers');

    function cmp($a, $b)
    {
	if ($a['quantity'] == $b['quantity']) {
	    return 0;
	}
	return ($a['quantity'] < $b['quantity']) ? 1 : -1;
    }

    uasort($numbers_by_positions, 'cmp');
    $numbers_by_positions = array_slice($numbers_by_positions, 0, $config['granted'], true);
    //var_dump($numbers_by_positions);
    $this->assign('numbers_by_positions', $numbers_by_positions);

    uasort($numbers_frequent, 'cmp');
    $numbers_frequent = array_slice($numbers_frequent, 0, $config['granted'], true);
    //var_dump($numbers_frequent);
    $this->assign('numbers_frequent', $numbers_frequent);
{/php}

	    <table id="position_analysis" border="1">
		<thead>Наиболее частые</thead>
		<tr>
		    <td></td>
		    <td>по позициям</td>
		    <td>вообщем</td>
		</tr>


		{section name="result" loop=$config.total+1}
		    {assign var="iteration" value=$smarty.section.result.iteration}
		    {if isset($numbers_by_positions[$iteration]) or isset($numbers_frequent[$iteration])}
		    <tr>
			<td>
			    {if isset($numbers_by_positions[$iteration])}
				{$numbers_by_positions[$iteration].number}
			    {else}
				{$numbers_frequent[$iteration].number}
			    {/if}
			</td>
			<td>
			{if isset($numbers_by_positions[$iteration])}
			    {$numbers_by_positions[$iteration].quantity}
			{/if}
			</td>
			<td>
			{if isset($numbers_frequent[$iteration])}
			    {$numbers_frequent[$iteration].quantity}
			{/if}
			</td>
		    </tr>
		    {/if}
		{/section}

	    </table>


	</td>

	<td valign="top">

{php}
    $config = Kernel_Config::get('default', 'numbers');
    $i = 0; $squeezing = array();
    while ($i < $config['total']) {
	if (isset($numbers_frequent[$i]) && isset($numbers_by_positions[$i])) {
	    $squeezing[$i] = array
		(
		    'number' => $i, 
		    'in_position' => $numbers_by_positions[$i]['quantity'],
		    'in_common' => $numbers_frequent[$i]['quantity'],
		);
	}
	$i ++;
    }

    function cmp_squeezing($a, $b)
    {
	if ($a['in_position'] == $b['in_position']) {
	    if ($a['in_common'] == $b['in_common']) {
		return 0;
	    }
	    return ($a['in_common'] < $b['in_common']) ? 1 : -1;
	}
	return ($a['in_position'] < $b['in_position']) ? 1 : -1;
    }
    uasort($squeezing, 'cmp_squeezing');

    $squeezing = array_slice($squeezing, 0, $config['squeezing_limit'], true);
    //var_dump($squeezing);
    $this->assign('squeezing', $squeezing);
{/php}


	    <table id="squeezing" border="1">
		<thead>Выжимка</thead>

		{foreach from=$squeezing item="entry"}
		<tr>
		    <td>{$entry.number}</td>
		</tr>
		{/foreach}

	    </table>

	</td>
	<td valign="top">


{php}
    $squeezing = array_keys($squeezing);
    $config = Kernel_Config::get('default', 'numbers');
    $combinations = $combination = array();
    $offset = 0; $length = $config['combination_strength'];

     do {
	$tmp = array_slice($squeezing, $offset, $length);
	if (4 == ($count = count($tmp))) {
	    $combination = $tmp;
	    
	} else {
	    $combination = $tmp;
	    $combination = array_merge($combination,
		array_slice($squeezing, 0, $length-$count));
	}

	if (in_array($combination, $combinations)) {
	    break;
	}
	$combinations[] = $combination;
	$offset ++;
    } while(true);
    
    $this->assign('combinations', $combinations);
    //var_dump($combinations);
{/php}

	    <table id="combinations" border="1">
		<thead>Комбинации</thead>

		{foreach from=$combinations item="entry" name="combinations"}
		    <tr>
			<td><strong>{$smarty.foreach.combinations.iteration}</strong></td>
			<td>
			{foreach from=$entry item="number"}
			    {$number} 
			{/foreach}
			</td>
		    </tr>
		{/foreach}
	    </table>
	</td>

    </tr>
</table>

